<?php
namespace api\xcx\controller;
use think\Cache;
use think\Loader;
use think\Db;
use think\Request;
use think\Controller;
use think\config;
class Shop extends Base
/**
 *  Shop.php 商店控制器
 *
 * @copyright			(C) 2014-2018 ShengTeng
 * @lastmodify			2018-05-21
 */

{
   /**
 * @api {POST} Shop/shopList 店列表
 * @apiVersion 1.0.0
 * @apiGroup 门店
 *
 * @apiParam {Int} page 页数 1
 * @apiParam {String} lng 经度(非必须)
 * @apiParam {String} lat 纬度(非必须)
 * @apiParam {String} token 用户token(非必须)
 *
 * @apiSuccess {Int} code 返回码
 * @apiSuccess {String} message  提示信息
 * @apiSuccess {Object} data  返回数据
 *
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *          "code":0,
 *          "message":"",
 *          "data":{
 *              shopId 店id
*               shopName 店名称,
*               address 店地址,
*               shop_status 店状态 1：正常。2：禁用,
*               pic 店图片,
*               shop_pic_status 店图片状态 1：正常。2：禁用,
*               numbers 服务人员,
*               serveringNumbers 正在服务人员,
*               waitingServer 等待服务
*               shop_type       上门状态 1：可以上门，2：禁止上门
*               distance      距离6.11单位(km)
*               isOrder        是否预约 0没有预约,大于0有预约
*               pics           图片,
*               orderNumber     订单号
*           }
*      }
*/
   public function shopList(Request $request){
        $params=$request->param();
        $Shop = Model('Shop');
        
        $res = [];
        if(!isset($params['page'])){
            return json(returnAjax('','缺少参数page','1'));
        }
        $page = $params['page'];
        // if(!isset($params['regionId'])){
        //     return json(returnAjax('','缺少参数regionId','1'));
        // }
        // $regionId = $params['regionId'];
        $regionId = 1;
        // if(!isset($params['lng'])){
        //     return json(returnAjax('','缺少参数lng','1'));
        // }
        $lng = $params['lng'];
        // if($lng == null || $lng == ""){
        //     return json(returnAjax('','lng不可为空','1'));
        // }
        // if(!isset($params['lat'])){
        //     return json(returnAjax('','缺少参数lat','1'));
        // }
        $lat =$params['lat'];
        // if($lat == null || $lat == ""){
        //     return json(returnAjax('','lat不可为空','1'));
        // }
        $token = $params['token'];

        $res = $Shop->selShopList($regionId,$page,$lng,$lat);
        $order = Model('Order');
        $shop_server  = Model('ShopServer');
        if($res || $res==[]){
            if(count($res)>0){
                // 如果有token字段,查看是否预约店
                for($i = 0;$i<count($res);$i++){
                    // 判断门店中是否有上班人员
                    $workingNum = $shop_server->selDetail($res[$i]['shopId']);
                    if($workingNum['numbers']>0){
                        // 获取设置门店等待人数
                        $watNum = $order->getWatingNum($res[$i]['shopId']);
                        // 设置门店等待人数
                        $setWatNum = $shop_server->setWatNum($res[$i]['shopId'],$watNum);
                        if(!$setWatNum){
                            return json(returnAjax('','设置门店等待人数失败','-1'));
                        }
                      	$shop_number =  $workingNum['numbers'];
                      	$shop_working = $workingNum['serveringNumbers'];
                        $res[$i]['waitingServer'] = $watNum;
                        $res[$i]['numbers'] = $shop_number;
                        $res[$i]['serveringNumbers'] = $shop_working>$shop_number?$shop_number:$shop_working;
                    }else{
                    	$res[$i]['waitingServer'] = $workingNum['waitingServer'];
                      	$shop_number =  $workingNum['numbers'];
                      	$shop_working = $workingNum['serveringNumbers'];
                        $res[$i]['numbers'] = $shop_number;
                        $res[$i]['serveringNumbers'] = $shop_working>$shop_number?$shop_number:$shop_working;
                    }
                    $res[$i]['isOrder'] = 0;
                    $res[$i]['orderNumber'] = 0;
                    $res[$i]['pics'] = [];
                    $pic = Model('ShopPic');
                    $pics = $pic->selByShopId($res[$i]['shopId']);
                    foreach($pics as $pics_l){
                        array_push($res[$i]['pics'],$pics_l['pic']);
                    }
                    if($token && count($token)>0){
                        // 根据token获取userId
                        $userId = $this->getUserId($token);
                        $order = Model('Order');
                        $is_order = $order->ordering($userId);
                        if($is_order){
                            $shopId = $is_order['shopId'];
                            if($res[$i]['shopId'] == $shopId){
                                $res[$i]['isOrder'] = $is_order['shopId'];
                                $res[$i]['orderNumber'] = $is_order['orderNumber'];
                            }
                        }
                    }
                }
            }
            return json(returnAjax($res));
        }else{
            return json(returnAjax('','查询数据失败','-1'));
        }
   }

     /**
     * @api {POST} Shop/commodityList 根据门店id获取项目列表
     * @apiVersion 1.0.0
     * @apiGroup 门店
     * 
     * @apiParam {Int} shopId 门店id
     *
     * @apiSuccess {Int} code 返回码
     * @apiSuccess {String} message  提示信息
     * @apiSuccess {Object} data  返回数据
     *
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "code":0,
     *          "message":"",
     *          "data":{
     *            "id"              项目id
     *            "name"            项目名称
     *            "shopId"          门店id
     *            "shopMoney"       正常价格
     *            "discountMoney"   vip价格
     *            "status"          状态1正常,2禁用
     *            "commondityId"    服务id,1为纯色美甲,2为款式美甲,其他
     *           }
     *      }
     */
    public function commodityList(Request $request){
        $params=$request->param();
        // 验证字段
        if(!isset($params['shopId'])){
            return json(returnAjax('','缺少参数shopId','1'));
        }
        $shopId = $params['shopId'];
        // 查看店铺是否存在
        $isShop = $this->selShop($shopId);
        if(!$isShop){
            return json(returnAjax('','店铺不存在',1));
        }
        $shopConfig = Model('ShopConfig');
        $res = $shopConfig->selByShopId($shopId);
        if($res || $res==[]){
            return json(returnAjax($res));
        }else{
            return json(returnAjax('','系统异常','-1'));
        }
    }


     /**
     * @api {POST} Shop/shopListWork 门店列表(员工上班)
     * @apiVersion 1.0.0
     * @apiGroup 门店
     * 
     * @apiParam {Int} page 页数
     * 
     * @apiSuccess {Int} code 返回码
     * @apiSuccess {String} message  提示信息
     * @apiSuccess {Object} data  返回数据
     *
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "code":0,
     *          "message":"",
     *          "data":{
     *               shopId 门店id,
     *               shopName 门店名称,
     *               shop_status 门店状态
     *           }
     *          备注(这个分页条数是100)
     *      }
     */
    public function shopListWork(Request $request){
        $params=$request->param();
        // page
        $page = $this->checkParam($params['page'],'page',true);
        if($page<=0){
            return json(returnAjax(null,'page格式错误',1));
        }
        $shop = Model('Shop');
        $res = $shop->shopListWork($page);
        if($res || $res==[]){
            return json(returnAjax($res));
        }else{
            return json(returnAjax(null,'系统异常',-1));
        }
    }
}
